import json,os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','untitled2.settings')
import django
django.setup()
from backend import run

from backend import models
class task_parse(object):
    def __init__(self,request):
        self.user=request.user
        self.request=json.loads(request.POST.get('data'))
        self.hosts_id=self.request['host_ids']
        print('---------hosts_id',self.hosts_id)
        self.type=self.request['type']
        self.command=self.request['cmd']
    def parse(self):
        if hasattr(self,self.type):
            task=getattr(self,self.type)
            return task()
    def cmd(self):
        task_obj=models.Task.objects.create(
            content=self.command,
            type='cmd',
            user=self.user
        )
        querylist=[]

        for host in self.hosts_id:

            obj=models.Task_res(
                task=task_obj,
                status=0,
                host_id=host,
                res='正在执行，请稍后！！'
            )
            querylist.append(obj)
        models.Task_res.objects.bulk_create(querylist)
        run.cmd(task_obj)

        return task_obj

    def file(self):
        pass
